package com.chensi.controller;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.chensi.common.AjaxJson;
import com.chensi.common.Constants;
import com.chensi.common.LoginUser;
import com.chensi.common.Pager;
import com.chensi.model.User;
import com.chensi.service.IUserService;
import com.chensi.util.BeanUtils;
import com.chensi.util.DateUtils;
import com.chensi.util.MD5Util;
import com.chensi.util.WebUtil;

@Controller
@RequestMapping("user")
public class UserController {

	@Autowired
	private IUserService userService;

	/**
	 * 页面
	 * 
	 * @return
	 */
	@RequestMapping("main")
	public String main() {
		return "user/main";
	}

	/**
	 * 详情页面
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("detailView")
	public ModelAndView detailView(Integer id) {
		User entity = userService.get(id);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("user", entity);
		return new ModelAndView("user/detail", map);
	}

	/**
	 * 添加页面
	 * 
	 * @return
	 */
	@RequestMapping("addView")
	public ModelAndView addView() {
		return new ModelAndView("user/edit");
	}

	/**
	 * 修改页面
	 * 
	 * @return
	 */
	@RequestMapping("updateView")
	public ModelAndView updateView(User user) {
		User entity = userService.get(user.getId());
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("user", entity);
		return new ModelAndView("user/edit", map);
	}

	// ======================================================================

	/**
	 * 获取详情
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("get.do")
	@ResponseBody
	public AjaxJson get(Integer id) {
		if (id == null) {
			return AjaxJson.getFailInfo(Constants.NOT_BLANK);
		}
		return AjaxJson.getSuccessInfo(userService.get(id));
	}
	
	/**
	 * 
	 * @return
	 */
	@RequestMapping({ "list.do", "list.json" })
	@ResponseBody
	public AjaxJson list() {
		return AjaxJson.getSuccessInfo(userService.list());
	}

	/**
	 * 筛选加载列表
	 * 
	 * @return
	 */
	@RequestMapping("page.do")
	@ResponseBody
	public AjaxJson page(User user, Pager pager) {
		return AjaxJson.getSuccessInfo(userService.pageByEntity(user, pager));
	}

	/**
	 * 新增
	 * 
	 * @return
	 */
	@RequestMapping("add.do")
	@ResponseBody
	public AjaxJson add(User user) {
		if (user.getRoleId() == null || StringUtils.isAnyBlank(user.getUsername(), user.getTel())) {
			return AjaxJson.getFailInfo(Constants.NOT_BLANK);
		}
		User param = new User();
		param.setUsername(user.getUsername());
		User tmp = userService.getByEntity(param);
		if (tmp != null) {
			return AjaxJson.getFailInfo(Constants.USER_EXIST);
		}
		if (StringUtils.isBlank(user.getPassword())) {
			user.setPassword(MD5Util.GetMD5Code(Constants.DEFAULT_PASSWORD));
		} else {
			user.setPassword(MD5Util.GetMD5Code(user.getPassword()));
		}
		// 默认待审核
		user.setIsSuper(Constants.USER_SUPER_N);
		user.setCreateTime(DateUtils.getNowTime());
		userService.save(user);
		return AjaxJson.getSuccessInfo();
	}

	/**
	 * 修改
	 * 
	 * @param user
	 * @return
	 */
	@RequestMapping("update.do")
	@ResponseBody
	public AjaxJson update(User user) {
		if (user.getId() == null) {
			return AjaxJson.getFailInfo(Constants.NOT_BLANK);
		}
		User param = new User();
		param.setUsername(user.getUsername());
		param.setIdNe(user.getId());
		User tmp = userService.getByEntity(param);
		if (tmp != null) {
			return AjaxJson.getFailInfo(Constants.USER_EXIST);
		}
		User old = userService.get(user.getId());
		BeanUtils.copyNotNullProperties(user, old);
		userService.update(old);
		return AjaxJson.getSuccessInfo();
	}

	/**
	 * 删除
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("remove.do")
	@ResponseBody
	public AjaxJson remove(Integer id) {
		if (id == null) {
			return AjaxJson.getFailInfo(Constants.NOT_BLANK);
		}
		// 校验允许删除
		User tmp = userService.get(id);
		LoginUser cur = (LoginUser) WebUtil.getSession().getAttribute(Constants.LOGIN_USER);
		if (cur.getId() == id) {
			return AjaxJson.getFailInfo(Constants.NOT_DELETE_SELF);
		}
		if (tmp.getIsSuper() == Constants.USER_SUPER_Y) {
			return AjaxJson.getFailInfo(Constants.NOT_DELETE_ADMIN);
		}
		userService.remove(id);
		return AjaxJson.getSuccessInfo();
	}

	/**
	 * 重置密码
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("resetPwd.do")
	@ResponseBody
	public AjaxJson resetPwd(Integer id) {
		if (id == null) {
			return AjaxJson.getFailInfo(Constants.NOT_BLANK);
		}
		User tmp = userService.get(id);
		tmp.setPassword(MD5Util.GetMD5Code(Constants.DEFAULT_PASSWORD));
		userService.update(tmp);
		return AjaxJson.getSuccessInfo();
	}


}
